

package com.scalar.chiptrack.operations.packingslip.actions;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.ScalarException;
import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.commons.Constants;
import com.scalar.chiptrack.operations.packingslip.dao.PackingSlipDAO;
import com.scalar.chiptrack.sales.dao.SalesOrderDAO;


public final class PackingSlipMenuAction extends ChipTrackAction {

    public ActionForward execute (ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response) 
                                  throws Exception {

        String action = request.getParameter("action");

        if ("view".equalsIgnoreCase (action)) {
            
            int customerShippingId = -1;
            
            try { 
                customerShippingId = Integer.parseInt (request.getParameter ("customerShippingId")); 
            }
            catch (NumberFormatException nfe) {
                // Invalid Id
            }

            request.setAttribute ("customerShippingId", customerShippingId+"");
            

			if (PackingSlipDAO.selectPackingSlipType ( customerShippingId ))
			{
				return (mapping.findForward ("viewPackingSlip"));//forwards to the same packing slip pages
			} 
			else
			{
				return (mapping.findForward ("viewDropShipment"));//forwards to the dropshipment pages
			}
        }
        
        
        if ("loadPackingSlips".equalsIgnoreCase(action)) {
            int salesOrderId = -1;
            
            try {
                salesOrderId = Integer.parseInt (request.getParameter ("salesOrderId"));
            }
            catch (NumberFormatException nfe) {
                // invalid salesOrderId
            }
                        
            if (salesOrderId != -1) {
                ArrayList packingSlipListBySalesOrderId = PackingSlipDAO.loadPackingSlipsBySalesOrderId (salesOrderId, Constants.CUSTOMER_SHIPPING_STATUS_ISSED);
                request.setAttribute ("packingSlipListBySalesOrderId", packingSlipListBySalesOrderId);

                ArrayList confirmedPackingSlipListBySalesOrderId = PackingSlipDAO.loadPackingSlipsBySalesOrderId (salesOrderId, Constants.CUSTOMER_SHIPPING_STATUS_SHIPPED);
                request.setAttribute ("confirmedPackingSlipListBySalesOrderId", confirmedPackingSlipListBySalesOrderId);
                
                ArrayList cancelledPackingSlipListBySalesOrderId = PackingSlipDAO.loadPackingSlipsBySalesOrderId (salesOrderId, Constants.CUSTOMER_SHIPPING_STATUS_CANCELLED);
                request.setAttribute ("cancelledPackingSlipListBySalesOrderId", cancelledPackingSlipListBySalesOrderId);
                
                request.setAttribute ("salesOrderId", salesOrderId+"");
            }
            
            return (mapping.findForward ("success"));
        }

        try {
            HttpSession session = request.getSession();
            
            // All Packingslips
            ArrayList packingSlipList = PackingSlipDAO.loadAllPackingSlips (Constants.CUSTOMER_SHIPPING_STATUS_ISSED);
            session.setAttribute ("packingSlipList", packingSlipList);

            // All Confirmed PackingSlips
            ArrayList confirmedPackingSlipList = PackingSlipDAO.loadAllPackingSlips (Constants.CUSTOMER_SHIPPING_STATUS_SHIPPED);
            session.setAttribute ("confirmedPackingSlipList", confirmedPackingSlipList);
            
            // All Cancelled PackingSlips
            ArrayList cancelledPackingSlipList = PackingSlipDAO.loadAllPackingSlips (Constants.CUSTOMER_SHIPPING_STATUS_CANCELLED);
            session.setAttribute ("cancelledPackingSlipList", cancelledPackingSlipList);
            
            //List salesOrderIds = PackingSlipDAO.loadAllPackingSlipSalesOrdersIds();
            ArrayList salesOrderIds = SalesOrderDAO.loadAllSalesOrderIdAndCustomerPOs();
            session.setAttribute ("salesOrderIds", salesOrderIds);
        }
        catch (ScalarException se) {
            // Exception while accessing database
            // log it
            se.getRootCause().printStackTrace();
        }
                
        return (mapping.findForward ("success"));
    }

} 